package net.tarine.gastronomi.client.forms;

import net.tarine.gastronomi.client.AuthSingleton;
import net.tarine.gastronomi.client.IAuthenticatedWidget;
import net.tarine.gastronomi.client.ParamMap;
import net.tarine.gastronomi.client.tables.DataModel;
import net.tarine.gastronomi.client.tables.GasUserTable;
import net.tarine.gastronomi.client.widgets.FramePanel;
import net.tarine.gastronomi.shared.Role;
import net.tarine.gastronomi.shared.to.GasUserTo;

import com.google.gwt.event.dom.client.MouseDownEvent;
import com.google.gwt.event.dom.client.MouseDownHandler;
import com.google.gwt.user.client.ui.Anchor;
import com.google.gwt.user.client.ui.VerticalPanel;

public class GasUserListPage extends FramePanel implements IAuthenticatedWidget {
	
	private GasUserTo user = null;
	private boolean editable = false;
	
	private VerticalPanel panel = null;
	
	public GasUserListPage(ParamMap params) {
		AuthSingleton.get().authorizeWidget(this);
	}
	@Override
	public void unlockWidget(GasUserTo user) {
		this.user=user;
		init();
	}
	
	private void init() {
		// Editing rights
		int roleId = user.getRoleId();
		if (Role.ADMIN.getId() == roleId) {
			editable=true;
		}
		// UI
		panel = new VerticalPanel();
		this.set(panel, "Utenti");
		drawResults();
	}
	
	private void drawResults() {
		panel.clear();
		Anchor createButton = new Anchor("Crea utente");
		if (editable) {
			panel.add(createButton);
		}		
		DataModel<GasUserTo> model = new GasUserTable.GasUserModel();
		final GasUserTable uTable = new GasUserTable(model);
		panel.add(uTable);
		//Listener sul bottone
		if (editable) {
			createButton.addMouseDownHandler(new MouseDownHandler() {
				@Override
				public void onMouseDown(MouseDownEvent event) {
					new GasUserPopUp("", uTable);
				}
			});
		}
	}

}
